require 'active_record/fixtures'
require '~/work/derbymatic/app/models/derbymatic'

class InitializeDerbymatic < ActiveRecord::Migration

  def self.up
    down

    wftda = InterleagueOrganization.create(
      :name => "Women's Flat Track Derby Association",
      :abbreviation => "WFTDA"
    )

    rcrg = League.create(
      :name => "Rat City Rollergirls",
      :abbreviation => "RCRG"
    )
    rcrg.interleague_organizations << wftda

    gd = Team.create(
      :name => "Grave Danger",
      :abbreviation => "GD",
      :league => rcrg
    )

    sw = Team.create(
      :name => "Sockit Wenches",
      :abbreviation => "SW",
      :league => rcrg
    )

    tr = Team.create(
      :name => "Throttle Rockets",
      :abbreviation => "TR",
      :league => rcrg
    )

    dlf = Team.create(
      :name => "Derby Liberation Front",
      :abbreviation => "DLF",
      :league => rcrg
    )

    rcr = League.create(
      :name => "Rose City Rollers",
      :abbreviation => "RCR"
    )
    rcr.interleague_organizations << wftda

    bb = Team.create(
      :name => "Break Neck Betties",
      :abbreviation => "BB",
      :league => rcr
    )

    hr = Team.create(
      :name => "High Rollers",
      :abbreviation => "HR",
      :league => rcr
    )

    hh = Team.create(
      :name => "Heartless Heathers",
      :abbreviation => "HH",
      :league => rcr
    )

    gnr = Team.create(
      :name => "Guns N Rollers",
      :abbreviation => "GNR",
      :league => rcr
    )

    add_skaters_by_name = proc { |tm, names|
      tm.skaters << names.map {|n| Skater.create(:name => n) }
    }
    add_skaters_by_name.call(gd, ["Rettig To Rumble", "Rollin' Bayou", "Jinx", "Edie Brickwall", "Sake Bomb"])
    add_skaters_by_name.call(bb, ["Rhea Deranged", "Terror Eyes", "Ava Sk8trix", "Dora Doom", "Fran Checkya"])

    gd.skaters.map {|s| $stdout.puts s.id}
    bb.skaters.map {|s| $stdout.puts s.id}

    bout1 = Bout.create(:title => "GD v BB")

    game1 = Game.create(:bout => bout1)
    game1.teams << [gd, bb]
    game1.game_teams[0].is_home_team = 'N'
    game1.game_teams.each { |gt|
      hv = gt.is_home_team
      game1.game_skaters << gt.team.skaters.map {|s|
        GameSkater.create(:skater => s, :is_home_team => hv)
      }
    }
    game1.periods << Period.create()
    game1.periods[0].jams << Jam.create()
    jam1 = game1.periods[0].jams[0]


    game1.save!

    

    game2 = Game.create(:bout => bout1)
    game2.teams << [sw, hh]
    game2.game_teams[1].is_home_team = 'N'
    game2.save!


    usa = Country.create(
      :name => "United States of America",
      :abbreviation => "USA"
    )
    wa = State.create(
      :name => "Washington",
      :abbreviation => "WA",
      :country => usa
    )
    sea = City.create(
      :name => "Seattle",
      :abbreviation => "SEA",
      :state => wa
    )


  end

  def self.down
    InterleagueOrganization.delete_all
    InterleagueMembership.delete_all
    League.delete_all
    Team.delete_all
    Skater.delete_all
    SkaterTeam.delete_all
    Staffer.delete_all
    InterleagueOrganizationStaffPosition.delete_all
    LeagueStaffPosition.delete_all
    TeamStaffPosition.delete_all
    Bout.delete_all
    Game.delete_all
    GameTeam.delete_all
    GameSkater.delete_all
    Period.delete_all
    Jam.delete_all
    Pass.delete_all
    SkaterJam.delete_all
    Penalty.delete_all
    SkaterJamPenalty.delete_all
    Location.delete_all
    City.delete_all
    State.delete_all
    Country.delete_all
  end

end

